import { useLocaleStoreWithOut } from "@/store/modules/locale";
import { i18n } from '@/locales'

const setI18nLanguage = (locale: LocaleType) => {
  const localeStore = useLocaleStoreWithOut()
  if (i18n.mode === 'legacy') {
    i18n.global.locale = locale
  } else {
    ; (i18n.global.locale as any).value = locale
  }
  // 设置当前语言
  localeStore.setCurrentLocale({
    lang: locale
  })
}

export const useLocale = () => {
  const changeLocale = async (locale: LocaleType) => {
    const globalI18n = i18n.global

    globalI18n.locale = locale

    setI18nLanguage(locale)
  }

  return {
    changeLocale
  }
}
